package com.ruoyi.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bizsys.domain.BankCode;
import com.ruoyi.bizsys.domain.LoanOrder;
import com.ruoyi.bizsys.domain.ProductInfo;
import com.ruoyi.bizsys.dto.UserInfo;
import com.ruoyi.bizsys.service.IBankCodeService;
import com.ruoyi.bizsys.service.ILoanOrderService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.bizsys.utils.UserCacheService;
import com.ruoyi.business.service.INoticeSmsService;
import com.ruoyi.common.utils.DesensitizeUtils;
import com.ruoyi.sms.domain.SmsSendData;
import com.ruoyi.sms.service.ISmsService;
import com.ruoyi.system.service.ISysConfigService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class INoticeSmsServiceImpl implements INoticeSmsService {

    private IBankCodeService bankCodeService;

    private UserCacheService userCacheService;

    private ISmsService smsService;

    private ILoanOrderService loanOrderService;

    private ISysConfigService sysConfigService;

    @Async("threadPoolTaskExecutor")
    @Override
    public void sendLoanNotice(LoanOrder loanOrder, String smsType) {
        log.info("准备发送短信");
        UserInfo userInfo = userCacheService.getUserInfoByCustNo(loanOrder.getCustNo());
        JSONObject smsParam = new JSONObject();
        smsParam.put("lastName", userInfo.getLastName());
        //SMS_TYPE_LOAN_FAILED 和 SMS_TYPE_BALANCE_INSUFFICIENT 的短信字段只有lastName
        if (BizConstants.SMS_TYPE_LOAN_SUCCESS.equals(smsType)) {
            ProductInfo productInfo = loanOrderService.selectLoanOrderProduct(loanOrder.getCustNo());
            if (productInfo != null) {
                String params = productInfo.getTags();
                if (params.contains("-2")) {
                    smsParam.put("credit_rating", sysConfigService.selectConfigKey(productInfo.getCreditRating()));
                    smsParam.put("amount", loanOrder.getLoanAmt().setScale(2, BigDecimal.ROUND_UP));
                    smsParam.put("Repayment_date", loanOrder.getRepayDate());
                    smsType = "main_wdrw_succ_dfr";
                }
            } else {
                smsParam.put("loanAmt", loanOrder.getLoanAmt());
                smsParam.put("repaymentDate", loanOrder.getRepayDate());
                List<BankCode> bankCodes = bankCodeService.list(new QueryWrapper<BankCode>().eq("paystack_bank_code", userInfo.getBankCode()));
                if (bankCodes.size() > 0) {
                    smsParam.put("bankName", bankCodes.get(0).getPaystackBankName());
                }
                smsParam.put("cardLast4", DesensitizeUtils.desensitize(loanOrder.getAccountNumber(), 3, 4));
            }
        }
        SmsSendData smsData = SmsSendData.builder().noticeType(BizConstants.SMS_NOTICE_TYPE_NOTICE).smsType(smsType).phoneNumber(userInfo.getRegisterPhone()).countryCode(userInfo.getCountryCode()).smsParam(smsParam).build();
        smsService.sendSms(smsData);

    }
}
